<?php

namespace app\admin\controller;

use app\admin\model\CarListModel;
use app\admin\model\ProgrammeModel;
use app\admin\validate\BannerValidate;
use app\admin\validate\CommonnValidate;
use app\BaseController;
use think\facade\Db;
use think\facade\Request as faRequest;

class CarList extends BaseController
{
    protected $middleware = ['Auth'];

    public function list() {
        $params = faRequest::post(['pageNo', 'pageSize', 'title', 'modelId']);
        $validate = new CommonnValidate();
        if (!$validate->scene('list')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $whereArr = [];
        if(isset($params['title']) && $params['title']) {
            $whereArr[] = ["title", "LIKE", '%'.$params['title'].'%'];
        }
        if(isset($params['modelId']) && $params['modelId']) {
            $whereArr[] = ["modelId", "=", $params['modelId']];
        }
        $sql = new CarListModel();
        $list = $sql
            ->where($whereArr)
            ->order('sort desc')
            ->order('create_at desc')
            ->paginate([
                'list_rows'=> request()->param('pageSize'),
                'page' => request()->param('pageNo'),
            ]);
        return result()->success($list);
    }

    public function addEdit() {
        $params = faRequest::post(['id']);
        if(isset($params['id']) && $params['id']) {
            return $this->add();
        }
        return $this->edit();
    }

    public function add() {
        $params = faRequest::post(['title', 'illustrate', 'image', 'video', 'modelId', 'kilo', 'registration', 'programme', 'minPrice', 'sort']);
        $validate = new BannerValidate();
        if (!$validate->scene('add')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        if(!isset($params['programme']) || count($params['programme']) <= 0) {
            return result()->fail("请添加租车方案", 500);
        }
        $programmeList = $params['programme'];
        foreach ($programmeList as $key => $item) {
           if(empty($item['day']) || empty($item['amount']) || empty($item['deposit'])) {
               return result()->fail("租车方案格式不正确", 500);
           }
        }
        Db::startTrans();
        try {
            $sql = new CarListModel();
            unset($params['programme']);
            $sql->save($params);
            foreach ($programmeList as $key => $item) {
                $item['carId'] = $sql->id;
            }
            $fanSql = new ProgrammeModel();
            $fanSql->saveAll($programmeList);
            Db::commit();
            return result()->success("添加成功");
        }  catch (\Throwable $th) {
            Db::rollback();
            return result()->fail($validate->getError(), 500);
        }
    }

    public function edit() {
        $params = faRequest::post(['id', 'title', 'illustrate', 'image', 'video', 'modelId', 'kilo', 'registration', 'programme', 'minPrice', 'sort']);
        $validate = new CommonnValidate();
        if (!$validate->scene('edit')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        if(!isset($params['programme']) || count($params['programme']) <= 0) {
            return result()->fail("请添加租车方案", 500);
        }
        Db::startTrans();
        try {
            $programmeList = $params['programme'];
            foreach ($programmeList as $key => $item) {
                if(empty($item['day']) || empty($item['amount']) || empty($item['deposit'])) {
                    return result()->fail("租车方案格式不正确", 500);
                }
                $item['carId'] = $params['id'];
            }
            $sql = new CarListModel();
            $info = $sql->find($params['id']);
            $info->save($params);
            return result()->success("修改成功");
        } catch (\Throwable $th) {
            Db::rollback();
            return result()->fail($validate->getError(), 500);
        }
    }

    public function dels() {
        $params = faRequest::post(['ids']);
        $validate = new CommonnValidate();
        if (!$validate->scene('dels')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new CarListModel();
        $sql->destroy($params['ids']);
        return result()->success("删除成功");
    }
}